//
//  CommonTool.m
//  KuWoCan
//
//  Created by hwd on 17/3/16.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "CommonTool.h"

@implementation CommonTool

+ (UIViewController *)getCurrtenViewCtrWithView:(id)view
{
    for (UIView* next = [view superview]; next; next = next.superview)
    {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
        {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}


//判断是否全是空格
+ (BOOL) isEmpty:(NSString *) str {
    if (!str) {
        return true;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}


+ (BOOL)checkStringIsEmpty:(NSString *) str
{
    if (!str)
    {
        return true;
    }
    else
    {
        if (str.length > 0)
        {
            NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
            NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
            if ([trimedString length] == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return true;
        }
    }
}



//正则过滤标签元素,还你纯净文本
+ (NSString *)flattenHTML:(NSString *)string
{
    NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n" options:0 error:nil];
    string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
    string = [string stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    return string;
}

//将对象转换成json
+ (NSString *)beJsonStrWithObj:(id)obj
{
    NSData *data=[NSJSONSerialization dataWithJSONObject:obj options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"    " withString:@""];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"(\\n)"
                                                 withString:@"" options:NSRegularExpressionSearch
                                                      range:NSMakeRange(0, [jsonStr length])];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"(\")"
                                                 withString:@"\'" options:NSRegularExpressionSearch
                                                      range:NSMakeRange(0, [jsonStr length])];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"(\\)"
                                                 withString:@"" options:NSRegularExpressionSearch
                                                      range:NSMakeRange(0, [jsonStr length])];
    return jsonStr;
}

//获取当前VC的上个VC
+ (UIViewController *)getLastViewControllerWithCurrentViewController:(UIViewController *)currentVC
{
    NSArray *vcsArray = [currentVC.navigationController viewControllers];
    if (vcsArray)
    {
        NSInteger vcCount = vcsArray.count;
        if (vcCount && vcCount>=2)
        {
            UIViewController *lastVC = vcsArray[vcCount-2];
            return lastVC;
        }
    }
    return nil;
}



@end
